from pyspades.server import block_action
from pyspades.collision import distance_3d_vector
from commands import add, admin
from map import Map
from pyspades.constants import *
import commands

@admin
def delwall(connection, value = None):
        connection.delwalling = value
        return 'dewalling %s block high wall' % connection.walling
add(delwall)

def apply_script(protocol, connection, config):
    class DWallMakerConnection(connection):
        delwalling = None
        def on_block_removed(self, x, y, z):
            if self.delwalling is not None:
                for wall_z in xrange(z - int(self.delwalling) + 1, z):
                    if x > 0 and x < 512 and y > 0 and y < 512 and wall_z > 0 and wall_z < 63:
                                block_action.x = x
                                block_action.y = y
                                block_action.z = wall_z
                                block_action.value = 1
                                self.protocol.send_contained(block_action, save = True)
                                self.protocol.map.remove_point(x, y, wall_z)
                                
            return connection.on_block_removed(self, x, y, z)
    return protocol, DWallMakerConnection